﻿using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class CJurisdictionDal
    {
        private string URL;
        public CJurisdictionDal()
        {
            dynamic jsonObject = JsonConvert.DeserializeObject(File.ReadAllText("appsettings.json"));
            URL = jsonObject.ConnectionStrings.SqlServer;
        }

        /// <summary>
        /// 创建菜单(找子级数据)
        /// </summary>
        /// <param name="quans"></param>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public async Task<List<Model.CJurisdiction>> CreateMainMenu(List<Jurisdiction> quans, int groupID)
        {
            List<Jurisdiction> quan1 = quans.Where(e => e.GroupID == groupID).ToList();
            List<Model.CJurisdiction> eQuans = new List<Model.CJurisdiction>();
            foreach (Jurisdiction item in quan1)
            {
                Model.CJurisdiction cj = new Model.CJurisdiction()
                {
                    JurID = item.JurID,
                    JurName = item.JurName,
                    JurAddress = item.JurAddress,
                    Icon = item.Icon,
                    List = await CreateMainMenu(quans, item.JurID)
                };
                eQuans.Add(cj);
            }
            return eQuans;
        }

        /// <summary>
        /// 获取菜单页面
        /// </summary>
        /// <param name="rolesID"></param>
        /// <returns></returns>
        public async Task<List<Model.CJurisdiction>> GetMainMenu(int rolesID)
        {
            using (SqlConnection con = new SqlConnection(URL))
            {
                try
                {
                    string sql = $@"select j.jur_id,jur_name,jur_address,group_id,icon from sys_jurisdiction j 
                                inner join sys_roles_jurisdiction rj on j.jur_id = rj.jur_id where rj.roles_id = {rolesID}";
                    List<Jurisdiction> list = (await con.QueryAsync<Jurisdiction>(sql)).ToList();
                    List<Model.CJurisdiction> cwp = await CreateMainMenu(list, 0);
                    return cwp;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        /// <summary>
        /// 根据userid查询RolesID
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task<int> GetRolesIDByUsersID(int uid)
        {
            using (SqlConnection con = new SqlConnection(URL))
            {
                try
                {
                    string sql = $"select roles_id from sys_user_roles where users_id = {uid}";
                    return await con.QueryFirstOrDefaultAsync<int>(sql);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
    }
}